﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XDL.Framework.Screens;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XDL.Framework;
using Microsoft.Xna.Framework.Input;
using XDL.Framework.Particles;

namespace JumpNFight.Screens
{
    class MainMenuScreen : MenuScreen
    {
        Timer m_timer = new Timer(TimeSpan.FromSeconds(1));
        Texture2D m_backgroundTexture;
        ParticleSystemCollection m_particleManager;
        Matrix m_projectionTransform;
        Matrix m_viewTransform = Matrix.Identity;

        SpriteBatch m_spriteBatch;
        ParticleSystem m_heartPS;
#if DEBUG
        Texture2D m_blank;
        Texture2D m_texture;
#endif
        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.0);
            TransitionOffTime = TimeSpan.FromSeconds(1.0);

            // static entries
            TextBlock line1 = new TextBlock("Crate");
            line1.Scale = 2;
            line1.FontName = "Fonts/TitleHeader";
            Controls.Add(line1);
            TextBlock line2 = new TextBlock("Mania");
            line2.Scale = 2;
            line2.FontName = "Fonts/TitleHeader";
            Controls.Add(line2);

            MenuItem playGameMenuEntry = new MenuItem("PLAY");
            playGameMenuEntry.Scale = 2;
            MenuItem optionsMenuEntry = new MenuItem("OPTIONS");
            optionsMenuEntry.Scale = 2;
            MenuItem exitMenuEntry = new MenuItem("EXIT");
            exitMenuEntry.Scale = 2;

            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            optionsMenuEntry.Selected += OptionsGameMenuEntrySelected;
            exitMenuEntry.Selected += ExitMenuEntrySelected;

            Controls.Add(playGameMenuEntry);
            Controls.Add(optionsMenuEntry);
            Controls.Add(exitMenuEntry);
        }

        #region Handle Input

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, EventArgs e)
        {
            LoadingScreen.Load(ScreenManager, LoadGameplayScreen, true);
        }

        /// <summary>
        /// Event handler for when the option menu is selected
        /// </summary>
        void OptionsGameMenuEntrySelected(object sender, EventArgs e)
        {
            OptionsPopup optionsScreen = new OptionsPopup();
            ScreenManager.AddScreen(optionsScreen);
        }

        /// <summary>
        /// Loading screen callback for activating the gameplay screen.
        /// </summary>
        void LoadGameplayScreen(object sender, EventArgs e)
        {
            GameplayScreen gameplayScreen = new GameplayScreen();
            gameplayScreen.ScreenManager = this.ScreenManager;
            gameplayScreen.Initialize();
            ScreenManager.AddScreen(gameplayScreen);
        }

        /// <summary>
        /// Event handler for when the Exit menu entry is selected.
        /// </summary>
        void ExitMenuEntrySelected(object sender, EventArgs e)
        {
            OnCancel();
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            MessageBoxScreen messageBox = new MessageBoxScreen("Exit Game?");
            messageBox.Accepted += ExitMessageBoxAccepted;
            ScreenManager.AddScreen(messageBox);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ExitMessageBoxAccepted(object sender, EventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        public override void LoadContent()
        {
            Viewport viewport = GameServices.Viewport;
            Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            m_projectionTransform = halfPixelOffset * projection;

            // retrieves the particle manager
            m_particleManager = ScreenManager.GetService<ParticleSystemCollection>();
            m_particleManager.SetCamera(m_viewTransform, m_projectionTransform);

            m_spriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
            m_backgroundTexture = GameServices.LoadContent<Texture2D>("Background");
            m_heartPS = GameServices.GetService<ParticleSystem>("Particles/Hearts");
#if DEBUG
            m_spriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
            m_blank = GameServices.LoadContent<Texture2D>("2d/Misc/Blank");
            m_texture = GameServices.LoadContent<Texture2D>("GreenGundam");
#endif
            base.LoadContent();
        }

        float sliceHeight = 12;

        public override Rectangle GetDrawArea()
        {
            return new Rectangle(12, 12, 400, 400);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if ( IsExiting )
            {
                // stop any effects
                m_particleManager.Clear();
            }
            else
            {
                m_timer.Update(gameTime);
                if (m_timer.IsSignaled)
                {
                    m_heartPS.AddParticle(new Vector3(431, 416, 0), -Vector3.UnitY);
                    m_timer.Reset();
                }
                m_particleManager.Update(gameTime);
            }
            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            m_spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone);
            m_spriteBatch.Draw(m_backgroundTexture, Vector2.Zero, Color.White);
            m_spriteBatch.End();

            m_particleManager.SetCamera(m_viewTransform, m_projectionTransform);
            m_particleManager.Draw(gameTime);

            base.Draw(gameTime);

        }
        #endregion
    }
}
